package com.ruoyi.develop.factory.factoryProduceWork.service.impl;

import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;

import javax.annotation.Resource;

import com.ruoyi.develop.factory.factoryProduceMain.domain.FactoryProduceMain;
import com.ruoyi.develop.factory.factoryProduceMain.mapper.FactoryProduceMainMapper;
import com.ruoyi.develop.goodsOrderIn.domain.GoodsOrderIn;
import com.ruoyi.develop.goodsOrderIn.mapper.GoodsOrderInMapper;
import com.ruoyi.develop.goodsSale.domain.GoodsSale;
import com.ruoyi.develop.goodsSale.mapper.GoodsSaleMapper;
import org.springframework.stereotype.Service;
import com.ruoyi.develop.factory.factoryProduceWork.mapper.FactoryProduceWorkMapper;
import com.ruoyi.develop.factory.factoryProduceWork.domain.FactoryProduceWork;
import com.ruoyi.develop.factory.factoryProduceWork.service.IFactoryProduceWorkService;

/**
 * 工单信息Service业务层处理
 *
 * @author qh
 * @date 2024-11-06
 */
@Service
public class FactoryProduceWorkServiceImpl implements IFactoryProduceWorkService {
    @Resource
    private FactoryProduceWorkMapper factoryProduceWorkMapper;
    @Resource
    private FactoryProduceMainMapper factoryProduceMainMapper;


    @Resource
    private GoodsSaleMapper goodsSaleMapper;



    /**
     * 查询工单信息
     *
     * @param id 工单信息主键
     * @return 工单信息
     */
    @Override
    public FactoryProduceWork selectById(String id) {
        return factoryProduceWorkMapper.selectById(id);
    }

    /**
     * 查询工单信息列表
     *
     * @param entity 工单信息
     * @return 工单信息
     */
    @Override
    public List<FactoryProduceWork> selectList(FactoryProduceWork entity) {
        return factoryProduceWorkMapper.selectList(entity);
    }

    @Override
    public List<FactoryProduceWork> listByPlan(FactoryProduceWork entity) {
        return factoryProduceWorkMapper.listByPlan(entity);
    }
    /**
     * 新增工单信息
     *
     * @param entity 工单信息
     * @return 结果
     */
    @Override
    public int insert(FactoryProduceWork entity) {
        if (StringUtils.isBlank(entity.getId())) {
            entity.setId(IdUtils.fastSimpleUUID());
        }
        if (StringUtils.isNotBlank(entity.getGoodsSaleId())) {
            GoodsSale goodsSale = goodsSaleMapper.selectById(entity.getGoodsSaleId());
            if( goodsSale != null){
                entity.setMaterialsCode(goodsSale.getGoodsCode());
                entity.setModelSpec(goodsSale.getGoodModel());
                entity.setGoodsId(goodsSale.getGoodsId());
                entity.setProductName(goodsSale.getGoodsName());
            }
        }
        if (StringUtils.isNotBlank(entity.getIpoNo())) {
            FactoryProduceMain factoryProduceMain =  factoryProduceMainMapper.selectByIpoNo(entity.getIpoNo());
            if(factoryProduceMain!=null){
                entity.setProjectId(factoryProduceMain.getProjectId());
                entity.setProNo(factoryProduceMain.getProjectCode());
            }
        }
        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        return factoryProduceWorkMapper.insert(entity);
    }

    /**
     * 修改工单信息
     *
     * @param entity 工单信息
     * @return 结果
     */
    @Override
    public int update(FactoryProduceWork entity) {
        if (StringUtils.isNotBlank(entity.getGoodsSaleId())) {
            GoodsSale goodsSale = goodsSaleMapper.selectById(entity.getGoodsSaleId());
            if( goodsSale != null){
                entity.setMaterialsCode(goodsSale.getGoodsCode());
                entity.setModelSpec(goodsSale.getGoodModel());
                entity.setGoodsId(goodsSale.getGoodsId());
                entity.setProductName(goodsSale.getGoodsName());
            }
        }
        if (StringUtils.isNotBlank(entity.getIpoNo())) {
            FactoryProduceMain factoryProduceMain =  factoryProduceMainMapper.selectByIpoNo(entity.getIpoNo());
            if(factoryProduceMain!=null){
                entity.setProjectId(factoryProduceMain.getProjectId());
                entity.setProNo(factoryProduceMain.getProjectCode());
            }
        }
        entity.setUpdateBy(SecurityUtils.getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());
        return factoryProduceWorkMapper.update(entity);
    }

    /**
     * 批量删除工单信息
     *
     * @param ids 需要删除的工单信息主键
     * @return 结果
     */
    @Override
    public int deleteByIds(String[] ids) {
        return factoryProduceWorkMapper.deleteByIds(ids);
    }

    /**
     * 删除工单信息信息
     *
     * @param id 工单信息主键
     * @return 结果
     */
    @Override
    public int deleteById(String id) {
        return factoryProduceWorkMapper.deleteById(id);
    }
}
